﻿using System.Threading.Tasks;

namespace Sidvall.WorkItemManager.Workflow
{
	public class WorkItemExecutionContext
	{
		public System.DateTime? LastCleanLogTime { get; set; }

		#region GetItemAsync

		public static async Task<WorkItemExecutionContext> GetItemAsync(string filePath)
		{
			WorkItemExecutionContext item;
			string content;

			try
			{
				content = await Sidvall.SystemContext.Current.SystemStorageManager.ReadAllTextAsync(filePath).ConfigureAwait(false);
				item = Sidvall.Serialization.SerializerManager.Current.Deserialize<WorkItemExecutionContext>(content);
			}
			catch
			{
				item = new WorkItemExecutionContext();
			}
			return item;
		}

		#endregion
		#region SaveItemAsync

		public async Task SaveItemAsync(string filePath)
		{
			string content;

			content = Sidvall.Serialization.SerializerManager.Current.SerializeObject(this);
			await Sidvall.SystemContext.Current.SystemStorageManager.WriteAllTextAsync(filePath, content).ConfigureAwait(false);
		}

		#endregion
	}
}
